package FactoryPatternEntity;
import javax.xml.parsers.*;

import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class Main {
    public static void main(String[] args) {
        //读取xml，涉及I/O操作，所以要用try。。。catch语句
        try {
            IAbstractCarFactory factory;

            //反射机制
            int legtn=ReadClassNameFromXML().toArray().length;
            for(int i=0;i<legtn;i++) {
                Class classReflection =
                        Class.forName(ReadClassNameFromXML().get(i));
                System.out.println();
                Object objcetReflection = classReflection.newInstance();


                factory = (IAbstractCarFactory) objcetReflection;
                Car car = factory.getCarEntity();
                System.out.printf(car.showName());
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }






    }

    public static List<String> ReadClassNameFromXML(){
          List<String> classNameList = new ArrayList<String>();

        try{

            //读取XML文件
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc;
            doc = builder.parse(new File("src\\FactoryPatternEnstance\\config.xml"));

            //获取包含类名的文本结点
            NodeList nl = doc.getElementsByTagName("className");
            //获取节点数目
//            Node classNode=nl.item(0).getFirstChild();
//            className=classNode.getNodeValue();
//            System.out.println("一共有" + nl.getLength() + "辆车");
            // 遍历每个节点
            for (int i = 0; i < nl.getLength(); i++) {

                // 索引从零开始
                Node classNode= nl.item(i).getFirstChild();
                // 获取节点所有属性集合
                classNameList.add(classNode.getNodeValue());



            }


        }catch (Exception e){
            e.printStackTrace();

        }finally {

        }
        return classNameList;



    }
}